<?php

	function validateRegistrationForm() {
		if (session_status() == PHP_SESSION_NONE) {
			session_start();
		};
		$flag      = TRUE;
		$mailRegex = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/";
		
		if (!isset($_POST["fname"]) || strcmp(trim($_POST["fname"]), "") == 0) {
			$flag = FALSE;
		} else {
			$_SESSION["fname"] = $_POST["fname"];
		};

		if (!isset($_POST["lname"]) || strcmp(trim($_POST["lname"]), "") == 0) {
			$flag = FALSE;
		} else {
			$_SESSION["lname"] = $_POST["lname"];
		};

		if (!isset($_POST["pass"]) || strcmp(trim($_POST["pass"]), "") == 0) {
			$flag = FALSE;
		} else {
			$_SESSION["pass"] = $_POST["pass"];
		};

		if (!isset($_POST["pass2"]) || strcmp(trim($_POST["pass2"]), "") == 0 || !isset($_POST["pass"]) || strcmp($_POST["pass"],$_POST["pass2"])!=0) {
			$flag = FALSE;
		} else {
			$_SESSION["pass2"] = $_POST["pass2"];
		};

		if (!isset($_POST["email"]) || strcmp(trim($_POST["email"]), "") == 0 ||  !preg_match($mailRegex, $_POST["email"])) {
			$flag = FALSE;
		} else {
				$_SESSION["email"] = $_POST["email"];
		};

		if (!isset($_POST["companyName"]) || strcmp(trim($_POST["companyName"]), "") == 0) {
			$flag = FALSE;
		} else {
				$_SESSION["companyName"] = $_POST["companyName"];		
		};

		if (!isset($_POST["companyCity"]) || strcmp(trim($_POST["companyCity"]), "") == 0) {
			$flag = FALSE;
		} else {
			$_SESSION["companyCity"] = $_POST["companyCity"];
		};

		if (!isset($_POST["companyAddress"]) || strcmp(trim($_POST["companyAddress"]), "") == 0) {
			$flag = FALSE;
		} else {
			$_SESSION["companyAddress"] = $_POST["companyAddress"];
		};

		if (!isset($_POST["respOfficial"]) || strcmp(trim($_POST["respOfficial"]), "") == 0) {
			$flag = FALSE;
		} else {
			$_SESSION["respOfficial"] = $_POST["respOfficial"];
		};

		if (!isset($_POST["shopName"]) || strcmp(trim($_POST["shopName"]), "") == 0) {
			$flag = FALSE;
		} else {
			$_SESSION["shopName"] = $_POST["shopName"];
		};

		if (!isset($_POST["shopCity"]) || strcmp(trim($_POST["shopCity"]), "") == 0) {
			$flag = FALSE;
		} else {
			$_SESSION["shopCity"] = $_POST["shopCity"];
		};

		if (!isset($_POST["phone"]) || strcmp(trim($_POST["phone"]), "") == 0) {
			$flag = FALSE;
		} else {
			$_SESSION["phone"] = $_POST["phone"];
		};

		if (!isset($_POST["shopAddress"]) || strcmp(trim($_POST["shopAddress"]), "") == 0) {
			$flag = FALSE;
		} else {
			$_SESSION["shopAddress"] = $_POST["shopAddress"];
		};
		if (!checkForExistingEntries()){
			$flag = FALSE;
		}
		if ($flag) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	function checkForExistingEntries() {
		if (isset($_POST["email"]) && strcmp(trim($_POST["email"]),"")!=0 ) {
			$fl=0;	
			$obj= new SelectUser();
			$fl = $obj->checkMail($_POST["email"]);
			if ($fl) {
				echo "<font color='red'>*зает email</font>";
				unset($_POST["email"]);
				return false;
			} else {
				echo "";
			}
			
		}
		if (isset($_POST["companyName"]) && strcmp(trim($_POST["companyName"]),"")!=0 ) {
			$fl=0;
			$obj= new SelectCompany();
			$fl = $obj->checkName($_POST["companyName"]);
			if ($fl) {
				echo "<font color='red'>*заето име на фирма</font>";
				unset($_POST["companyName"]);
				return false;
			} else {
				echo "";
			}
				
		}
		return true;
	}

	if (isset($_POST["ajaxCheck"])) {
		require_once '../mdl/Select/SelectUser.php';
		require_once '../mdl/Select/SelectCompany.php';
		checkForExistingEntries();
		unset($_POST["ajaxCheck"]);
	}


?>
